package org.trialiet.notedemo.activity;

import java.util.HashMap;
import java.util.Map;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import org.apache.http.Header;
import org.trialiet.notedemo.R;
import org.trialiet.notedemo.http.ObjectCallback;
import org.trialiet.notedemo.util.DBAdapter;
import org.trialiet.notedemo.util.HttpUtils;

public class LoginActivity extends Activity {
    private String loginUsername;
    private String loginPassword;
    private EditText etUsername;
    private EditText etPassword;
    private SharedPreferences sp;
    private final String url = "http://192.168.0.55:8080/NoteServer/login.action";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        etUsername = (EditText) findViewById(R.id.et_login_username);
        etPassword = (EditText) findViewById(R.id.et_login_password);
        Button btnLogin = (Button) findViewById(R.id.btn_login_login);
        Button btnRegister = (Button) findViewById(R.id.btn_login_register);
        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                loginUsername = etUsername.getText().toString();
                loginPassword = etPassword.getText().toString();
                AsyncHttpClient client = new AsyncHttpClient();
                RequestParams params = new RequestParams();
                params.add("username", loginUsername);
                params.add("password", loginPassword);
                client.post(getApplicationContext(), url, params, new TextHttpResponseHandler() {
                    @Override
                    public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                        Toast.makeText(LoginActivity.this, "We have some problem here :(", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onSuccess(int i, Header[] headers, String s) {
                        JsonParser jsonParser = new JsonParser();
                        JsonElement element = jsonParser.parse(s);
                        JsonObject root = element.getAsJsonObject();
                        JsonPrimitive flagJson = root.getAsJsonPrimitive("success");
                        boolean flag = flagJson.getAsBoolean();
                        if (flag){
                            JsonObject user = root.getAsJsonObject("user");
                            JsonPrimitive jsonUserId = user.getAsJsonPrimitive("user_id");
                            long user_id = jsonUserId.getAsLong();
                            sp = getSharedPreferences("LoginUser", MODE_PRIVATE);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putLong("user_id", user_id);
                            editor.apply();
                            Toast.makeText(LoginActivity.this, "Login Successfully", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(LoginActivity.this, ListActivity.class);
                            LoginActivity.this.startActivity(intent);
                            LoginActivity.this.finish();
                        }
                    }
                });
                DBAdapter dbAdapter = new DBAdapter(LoginActivity.this);
                dbAdapter.open();
                long user_id = dbAdapter.getUser(loginUsername,loginPassword);
                if (user_id > 0) {
                    sp = getSharedPreferences("LoginUser", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putLong("user_id", user_id);
                    editor.apply();
                    Toast.makeText(LoginActivity.this, "Login Successfully", Toast.LENGTH_LONG).show();
                    dbAdapter.close();
                    Intent intent = new Intent(LoginActivity.this, ListActivity.class);
                    LoginActivity.this.startActivity(intent);
                    LoginActivity.this.finish();
                }
                else{
                    Toast.makeText(LoginActivity.this, "We have some problem here :(", Toast.LENGTH_LONG).show();
                }
            }
        });
        btnRegister.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        switch (requestCode){
            case 1:
                if (resultCode == RESULT_OK){
                    etUsername.setText(data.getStringExtra("username"));
                    etPassword.setText(data.getStringExtra("password"));
                }
                break;
            default:
        }
    }
}

